package com.comum.javahomework420242025.controller;


import com.comum.javahomework420242025.pojo.*;


import com.comum.javahomework420242025.repository.TraceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TraceController {


    @Autowired
    private TraceRepository traceRepository;




    @PostMapping("/addTrace")
    public Result addTrace(@RequestBody Trace trace){


        traceRepository.save(trace);
        return Result.success();
    }
    @PutMapping("/updateTrace")
    public Result updateTrace(@RequestBody Trace trace){
        traceRepository.save(trace);
        return Result.success();
    }
    @DeleteMapping("/deleteTrace")
    public Result deleteTrace(String id){
        // TODO: 2024/12/23 Trace删除
        System.out.println(id);
        return Result.success();
    }





    @GetMapping("/getAllTrace")
    public Result getAllTrace()
    {
        return Result.success(traceRepository.findAll());
    }


    @PostMapping("/getAllTraceById")
    public Result getAllTraceById(@RequestBody AskList askList) {
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String findType = askList.getFindType();
        PageBean<Trace> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Trace> page = traceRepository.list(str, findType, pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());

        return Result.success(pb);
    }

    @PostMapping("/getAllChanceByUserIdTraceuser")
    public Result getAllChanceByUserId(@RequestBody AskList askList) {

        String str = askList.getStr();

        List<Chance> chances = traceRepository.getAllChanceByUserId(str);


        return Result.success(chances);
    }

}
